

```{r Library}

require(tidyverse)
require(haven)
require(janitor)
require(tidycensus)
require(survey)
require(srvyr)
require(patchwork)


## Tables
require(xtable)
require(reshape2)

## Graphs
require(ggrepel)
require(ggtext)

`%nin%` <- Negate(`%in%`)

## Addressing some code where there is only 1 PSU for MRP
options(survey.adjust.domain.lonely=TRUE)
options(survey.lonely.psu="adjust")

```



```{r Data load}

surveys <- read_csv("Data/survey.ps_merged.csv")

survey_list <- surveys %>%
  group_by(Year, Survey) %>%
  reframe(count=n())

```


```{r Data prep}

## Make sure partisanship is 3 point dummy

diag_df <- surveys %>%
  group_by(Party,
           Survey) %>%
  reframe(count=n())

## Following convention from other surveys:

## USE 7PT PID TO IDENTIFY INDEPENDENT AND LEANER
## ONLY DROP PEOPLE WHO "DON'T KNOW"/NA ON 7PT PID

surveys <- surveys %>%
  filter(is.na(Party)==FALSE) %>%
  filter(Party != "Other")

diag_df <- surveys %>%
  group_by(Party) %>%
  reframe(count=n())


## Make dummy

surveys <- surveys %>%
  mutate(Rep = ifelse(Party == "Republican",
                      1,
                      0),
         Ind = ifelse(Party == "Independent",
                      1,
                      0)
  )

diag_df <- surveys %>%
  group_by(Party,
           Dem,
           Rep,
           Ind
           ) %>%
  reframe(count=n())

## Make generation

diag_df <- surveys %>%
  group_by(birthyear,
           age) %>%
  reframe(count=n())

surveys <- surveys %>%
  mutate(birthyear = case_when(
    is.na(birthyear) == FALSE ~ birthyear,
    is.na(birthyear) == TRUE ~ Year - age,
    TRUE ~ NA)
  )
  
diag_df <- surveys %>%
  group_by(birthyear,
           age) %>%
  reframe(count=n())

surveys <- surveys %>%
  mutate(generation = case_when(
    birthyear < 1928 ~ "Greatest",
    birthyear > 1927 & 
      birthyear < 1946 ~ "Silent",
    birthyear > 1945 & 
      birthyear < 1965 ~ "Boomer",
    birthyear > 1964 & 
      birthyear < 1981 ~ "GenX",
    birthyear > 1980 & 
      birthyear < 1997 ~ "Millennial",
    birthyear > 1996 & 
      birthyear < 2012 ~ "GenZ",
    TRUE ~ "Missing")
  )

diag_df <- surveys %>%
  group_by(birthyear,
           age,
           generation) %>%
  reframe(count=n())


## Dropping outliers;
## Pew 2004

surveys <- surveys %>%
  filter(Survey != "Pew" | Year != 2004)

## final checks

diag_df <-  surveys %>%
  group_by(Survey,
           USborn) %>%
  reframe(count=n())

yearsurvey_list <- surveys %>%
  group_by(Year,Survey)%>%
  reframe(count=n())

## Checking weights

diag <- surveys %>%
  group_by(PSweights) %>%
  reframe(count=n())

surveys <- surveys %>%
  filter(PSweights > 0)


```



```{r Table 1}

## Showing generational differences over time 
## Between US and non-US born 

## Load Census data
census_1990 <- read_csv("Other Census Data/Census_1990_Shares.csv")
census_2000 <- read_csv("Other Census Data/Census_2000_Shares.csv")
census_2010 <- read_csv("Other Census Data/Census_2010_Shares.csv")
census_2020 <- read_csv("Other Census Data/Census_2020_Shares.csv")


## Merge and final clean

## Add Share variable (% per Year, All and USBorn)

census_1990_all <- census_1990 %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 1990) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "All")

census_1990_us <- census_1990 %>%
  filter(USborn == 1) %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 1990) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "US Born")



census_2000_all <- census_2000 %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 2000) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "All")

census_2000_us <- census_2000 %>%
  filter(USborn == 1) %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 2000) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "US Born")


census_2010_all <- census_2010 %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 2010) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "All")

census_2010_us <- census_2010 %>%
  filter(USborn == 1) %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 2010) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "US Born")


census_2020_all <- census_2020 %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 2020) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "All")

census_2020_us <- census_2020 %>%
  filter(USborn == 1) %>%
  group_by(generation) %>%
  reframe(WeightedN = sum(WeightedN),
          year = 2020) %>%
  mutate(Share = WeightedN/sum(WeightedN),
         Group = "US Born")

shares <- rbind(census_1990_all,
                census_1990_us,
                census_2000_all,
                census_2000_us,
                census_2010_all,
                census_2010_us,
                census_2020_all,
                census_2020_us)


## Add Group variable

shares <- pivot_wider(shares,
                      id_cols = c("year","Group"),
                      names_from = "generation",
                      values_from = "Share")



```


```{r Table 2}

## List of data

table2_df <- surveys %>%
  mutate(Survey = case_when(
    Survey == "CES" & Year < 2019 ~ "CCES",
    TRUE ~ Survey
  ))

table2surveys_df <- table2_df %>%
  group_by(Year, Survey) %>%
  reframe(count = n()) %>%
  pivot_wider(id_cols = Survey,
              names_from = Year,
              values_from = count)


table2means_df <- table2_df %>%
  group_by(Survey, Year) %>%
  reframe(count = n()) %>%
  ungroup() %>%
  group_by(Survey) %>%
  reframe(mean = mean(count))


```


```{r Figure 1: Full Sample}

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp)
  
  year_means <- surveys_temp %>%
    as_survey(Weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  
  
  print(i)
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
  
  
plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Dem_se)


plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Rep_se)


plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Ind_se)


## Rebind and plot

plot_df <- rbind(plot_dem_df,
                 plot_rep_df,
                 plot_ind_df)

## Finalize names

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Ind" ~ "Independent",
    PID == "Rep" ~ "Republican"
  ))

year_range <- unique(yearsurvey_list$Year)
  
fig1 <- ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  geom_point(aes(size = Survey_N/2000,
                 color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
    geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_size(guide = "none") + 
  scale_x_continuous(breaks = c(1989, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,85))+
  ylab("% Identifying With Party") +
  theme_bw() +
  theme(legend.position = "top")

## Add errorbars

# fig1 +  geom_errorbar(data = plot_df,
#                       aes(ymin = lower_ci,
#                     ymax = upper_ci,
#                     fill = "gray50"))
  

  
ggsave(height = 6, width = 8, filename = "Figures/fig1_pid_fullsample.jpg")


```


```{r Figure 2: US-born vs Foreign-born}


yearsurvey_list <- surveys %>%
  group_by(Year,Survey)%>%
  reframe(count=n())

  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
## US born first
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 1)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$USborn[1] <- "Born in US"
  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 1)
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$USborn[1] <- "Born in US"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
## CCES 2015 has small and very skewed sample, removing from results does not
## change trend line and avoids very different (and small) point estimate
plot_df <- plot_df %>%
  filter(Year %nin% c(2015) |
         Survey %nin% c("CES"))

plot_df <- plot_df %>%
  filter(Survey_N > 0)

plot_df_usborn <- plot_df


## Foreign Born

plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 0)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$USborn[1] <- "Born Outside US / in Puerto Rico"
  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 0)
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$USborn[1] <- "Born Outside US / in Puerto Rico"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
## CCES 2015 has small and very skewed sample, removing from results does not
## change trend line and avoids very different (and small) point estimate
plot_df <- plot_df %>%
  filter(Year %nin% c(2015) |
         Survey %nin% c("CES"))

plot_df <- plot_df %>%
  filter(Survey_N > 0)

plot_df_foreign <- plot_df



## Merge

plot_df <- rbind(plot_df_usborn,
                 plot_df_foreign)
  
  
plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)

plot_df$USborn <- factor(plot_df$USborn,
                         levels = c("Born Outside US / in Puerto Rico",
                                    "Born in US"))

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Dem_se)


plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Rep_se)


plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Ind_se)


## Rebind and plot

plot_df <- rbind(plot_dem_df,
                 plot_rep_df,
                 plot_ind_df)

## Finalize names

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Ind" ~ "Independent",
    PID == "Rep" ~ "Republican"
  ))

year_range <- unique(yearsurvey_list$Year)
  
fig2 <- ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  # geom_point(aes(size = Survey_N/2000,
  #                color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
    geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_size(guide = "none") + 
  scale_x_continuous(breaks = c(1989, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,80))+
  ylab("% Identifying With Party") +
  theme_bw() +
  facet_wrap(~USborn)+
  theme(legend.position = "top")


ggsave(height = 6, width = 8, filename = "Figures/fig2_pid_bynativity.jpg")

## Including point estimates in appendix figure

fig2 <- ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  geom_point(aes(size = Survey_N/2000,
                 color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
    geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_size(guide = "none") + 
  scale_x_continuous(breaks = c(1989, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,80))+
  ylab("% Identifying With Party") +
  theme_bw() +
  facet_wrap(~USborn)+
  theme(legend.position = "top")


ggsave(height = 6, width = 8, filename = "Figures/Appendix/fig2a_pid_bynativity_ptestimates.jpg")


```




```{r Figure 3: Generational US-born only}

## Create list of surveys to draw from dataset
## Results in individual survey-year point estimates

yearsurveygeneration_list <- surveys %>%
  filter(is.na(USborn)==FALSE) %>%
  group_by(Year,Survey,generation)%>%
  reframe(count=n()) %>%
  filter(is.na(generation)==FALSE) %>%
  
  ## N is too small for these groups, filter out
  filter(generation!= "Greatest") %>%
  filter(generation!= "GenZ" |
         Year %nin% c(2008,2012)) %>%
  filter( !(generation == "GenZ" & Year == 2015))


## Remove any without generation data

surveys <- surveys %>%
  filter(is.na(generation)==FALSE)


## Create tribble with ID variables and party ID estimamtes

  plot_df <- tribble(~Year,~Survey, ~Survey_N,~Generation, ~Dem, ~Dem_var, ~Rep, ~Rep_var, ~Ind, ~Ind_var,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  
## Next loop extracts data using svy package from surveys dataset
## Estimates, per survey-year, each of the three PID categories
  
  ## Start loop dataframe on item 1
  
  year_temp <- yearsurveygeneration_list$Year[1]
  survey_temp <- yearsurveygeneration_list$Survey[1]
  generation_temp <- yearsurveygeneration_list$generation[1]

  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           generation == generation_temp &
           USborn == 1)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem, vartype = c("var","ci")),
            Ind = survey_mean(Ind, vartype = c("var","ci")),
            Rep = survey_mean(Rep, vartype = c("var","ci")),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  plot_df$Generation<-generation_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_var[1] <- year_means$Dem_var[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_var[1] <- year_means$Rep_var[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_var[1] <- year_means$Dem_var[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  
plot_df_bin <- plot_df
plot_df <- plot_df_bin


## Loop over 2:end

for(i in 2:length(yearsurveygeneration_list$Year)){

  plot_temp <- tribble(~Year,~Survey, ~Survey_N,~Generation, 
                       ~Dem, ~Dem_var, 
                       ~Rep, ~Rep_var, 
                       ~Ind, ~Ind_var,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  year_temp <- yearsurveygeneration_list$Year[i]
  survey_temp <- yearsurveygeneration_list$Survey[i]
  generation_temp <- yearsurveygeneration_list$generation[i]
  

  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           generation == generation_temp &
           USborn == 1)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem, vartype = c("var","ci")),
            Ind = survey_mean(Ind, vartype = c("var","ci")),
            Rep = survey_mean(Rep, vartype = c("var","ci")),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp$Generation<-generation_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_var[1] <- year_means$Dem_var[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_var[1] <- year_means$Rep_var[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_var[1] <- year_means$Dem_var[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  
}

plot_df <- plot_df %>%
  filter(Survey_N > 0)

plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))



plot_df <- plot_df %>%
  mutate(var = case_when(
    PID == "Dem" ~ Dem_var,
    PID == "Ind" ~ Ind_var,
    PID == "Rep" ~ Rep_var
  ))

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Rep" ~ "Republican",
    PID == "Ind" ~ "Independent"
  ))

plot_df$`Party ID` <- factor(plot_df$`Party ID`,
                      levels = c("Democrat",
                                 "Independent",
                                 "Republican"))

## Making labels for beginning/end of lines

## Left first
labels1_df <- plot_df %>%
  filter( (Year == 1989 & Generation %in% c("Silent",
                                            "Boomer",
                                            "GenX") |
           (Year %in% c(2000, 2002) & Generation == "Millennial") |
           (Year == 2016 & Generation == "GenZ"))
  ) %>%
  group_by(Generation, `Party ID`) %>%
  reframe(leftvalue = mean(value)*100) %>%
  mutate(leftvalue = round(leftvalue, 0)) %>%
  mutate(leftvalue = ifelse(Generation == "Millennial" &
                              `Party ID` == "Democrat",
         60,
         leftvalue)) %>%
  mutate(leftlabel = paste(leftvalue, "%", sep = " "),
         Year = case_when(
           Generation %in% c("Silent",
                             "Boomer",
                             "GenX") ~ 1989,
           Generation == "Millennial" ~ 1999,
           Generation == "GenZ" ~ 2016
         )) 

  ## Position
  # mutate(leftpos = case_when(
  #   Generation %in% c("Silent",
  #                     "Boomer",
  #                     "GenX") ~ 1987,
  #   Generation == "Millennial" ~ 1997,
  #   Generation == "GenZ" ~ 2014))

## Right second
labels2_df <- plot_df %>%
  filter(Year %in% c(2022,2023)) %>%
  group_by(Generation, `Party ID`) %>%
  reframe(rightvalue = mean(value)*100) %>%
  mutate(rightvalue = round(rightvalue, 0)) %>%
  mutate(rightlabel = paste(rightvalue, "%", sep = " "),
         Year = 2023)

plot_df <- left_join(plot_df,
                     labels1_df,
                     by = c("Generation", "Party ID", "Year"))

## Fixing GenZ labels

plot_df <- plot_df %>%
  mutate(leftlabel = case_when(
    Generation == "GenZ" & Survey != "CES" ~ NA,
    TRUE ~ leftlabel
  ))

plot_df <- left_join(plot_df,
                     labels2_df,
                     by = c("Generation", "Party ID", "Year"))


year_range <- unique(yearsurvey_list$Year)


## Fix names, factor levels, axis range

plot_df <- plot_df %>%
  mutate(Generation = case_when(
    Generation == "Silent" ~ "Silent (`28-`45)",
    Generation == "Boomer" ~ "Boomer (`46-`64)",
    Generation == "GenX" ~ "GenX (`65-`80)",
    Generation == "Millennial" ~ "Millennial (`81-`96)",
    Generation == "GenZ" ~ "GenZ (`97-)"
  ))

plot_df$Generation <- factor(plot_df$Generation,
                             levels = c("Silent (`28-`45)",
                                        "Boomer (`46-`64)",
                                        "GenX (`65-`80)",
                                        "Millennial (`81-`96)",
                                        "GenZ (`97-)"))

ggplot(data = plot_df, 
               aes(x = Year,
               color = `Party ID`,
               y = value*100,
               ))+
  geom_smooth(method = "loess",
              aes(weight = 1/var))+
  ## Left label
  geom_text_repel(aes(label = leftlabel,
                      y = leftvalue,
                      x = Year),
                  direction = "y",
                  hjust = 0,
                  nudge_x = -2,
                  size = 3,
                  max.overlaps = 12,
                  box.padding = .3,
                  force = 1)+
  ## Right label
    geom_text_repel(aes(label = rightlabel,
                      y = rightvalue,
                      x = Year),
                  direction = "y",
                  hjust = 0,
                  nudge_x = 1,
                  size = 3,
                  max.overlaps = 10,
                  box.padding = .2,
                  force = 1)+
  geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_y_continuous(breaks = c(0, 20, 40, 60, 80, 100),
                     limits = c(-10,100),
                     name = "% Identifying With Party",
                     sec.axis = sec_axis(trans = ~.*1,
                                         breaks = c(0, 20, 40, 60, 80, 100),
                                         name = "% Identifying With Party"))+
  scale_color_manual(values = c("blue2",
                                "green4",
                                "red2"))+
  theme_bw()+
  theme(legend.position = "top")+
  facet_wrap(~Generation, ncol = 1)


ggsave(height = 8, width = 8, filename = "Figures/fig3_bygeneration_usborn_genfacet.jpg")



## Appendix figure with points


ggplot(data = plot_df, 
               aes(x = Year,
               color = `Party ID`,
               y = value*100,
               ))+
  geom_point(aes(size = Survey_N/2000,
                 color = `Party ID`)) +
  geom_smooth(method = "loess",
              aes(weight = 1/var))+
  ## Left label
  geom_text_repel(aes(label = leftlabel,
                      y = leftvalue,
                      x = Year),
                  direction = "y",
                  hjust = 0,
                  nudge_x = -2,
                  size = 3,
                  max.overlaps = 12,
                  box.padding = .3,
                  force = 1)+
  ## Right label
    geom_text_repel(aes(label = rightlabel,
                      y = rightvalue,
                      x = Year),
                  direction = "y",
                  hjust = 0,
                  nudge_x = 1,
                  size = 3,
                  max.overlaps = 10,
                  box.padding = .2,
                  force = 1)+
  geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_y_continuous(breaks = c(0, 20, 40, 60, 80, 100),
                     limits = c(-10,100),
                     name = "% Identifying With Party",
                     sec.axis = sec_axis(trans = ~.*1,
                                         breaks = c(0, 20, 40, 60, 80, 100),
                                         name = "% Identifying With Party"))+
  scale_color_manual(values = c("blue2",
                                "green4",
                                "red2"))+
  theme_bw()+
  theme(legend.position = "top")+
  facet_wrap(~Generation, ncol = 1)


ggsave(height = 8, width = 8, filename = "Figures/Appendix/fig3a_bygeneration_usborn_genfacet_witpoints.jpg")



```


```{r Figure 4: National Origin USborn only}


diag_df <- surveys %>%
  group_by(Survey,
           origin) %>%
  reframe(count=n())

yearsurvey_list <- surveys %>%
  group_by(Year,Survey)%>%
  reframe(count=n())

## Mexican first

  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Mexican",
           USborn==1)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$Origin[1] <- "Mexican"

  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Mexican",
                      USborn==1)

  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$Origin[1] <- "Mexican"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
plot_mex_df <- plot_df
  
  
## Then Puerto Rican
  
    plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Puerto Rican",
                      USborn==1)

  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$Origin[1] <- "Puerto Rican"

  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Puerto Rican",
                     USborn==1)

  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$Origin[1] <- "Puerto Rican"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
plot_pr_df <- plot_df
  
  
  
## Lastly Cuban

  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Cuban",
                     USborn==1)

  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$Origin[1] <- "Cuban"


for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Cuban",
                     USborn==1)

  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$Origin[1] <- "Cuban"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
plot_cuba_df <- plot_df


plot_df <- rbind(plot_mex_df,
                 plot_pr_df,
                 plot_cuba_df)

  
## Drop all results missing Natl Origin

plot_df <- plot_df %>%
  filter(Survey_N > 0)

## Drop overly sparse results too
plot_df <- plot_df %>%
  filter(Survey_N > 10)
  
  
plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Dem_se)


plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Rep_se)


plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Ind_se)


## Rebind and plot

plot_df <- rbind(plot_dem_df,
                 plot_rep_df,
                 plot_ind_df)

## Finalize names

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Ind" ~ "Independent",
    PID == "Rep" ~ "Republican"
  ))

plot_df$Origin <- factor(plot_df$Origin,
                         levels = c("Mexican",
                                    "Puerto Rican",
                                    "Cuban"))

year_range <- unique(yearsurvey_list$Year)
  
ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  # geom_point(aes(size = Survey_N/2000,
  #                color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
  scale_size(guide = "none") + 
    geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,100))+
  ylab("% Identifying With/Leaning Towards Party") +
  theme_bw() +
  facet_wrap(~Origin)+
  theme(legend.position = "top",
        axis.text.x = element_text(size = 7))


ggsave(height = 7, width = 12, filename = "Figures/fig4_pid_bynatlorigin.jpg")

## Including point estimates in appendix figure

fig2 <- ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  geom_point(aes(size = Survey_N/2000,
                 color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
    geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_size(guide = "none") + 
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,100))+
  ylab("% Identifying With/Leaning Towards Party") +
  theme_bw() +
  facet_wrap(~Origin)+
  theme(legend.position = "top")

ggsave(height = 7, width = 12, filename = "Figures/Appendix/fig4_pid_bynatlorigin_ptestimates.jpg")


```


```{r Appendix List of Datasets}

## Get full list of variables, sample size for each dataset
## Formatting manually in our LaTeX file


## Sample N

table_df <- surveys %>%
  group_by(Survey,
           Year) %>%
  reframe(count=n())


## Checking for other variables

table_df <- surveys %>%
  group_by(Survey,
           Year,
           USborn,
           origin) %>%
  reframe(count=n())


## Check region on its own

table_df <- surveys %>%
  group_by(Survey,
           Year,
           region,
           region2,
           region_pew) %>%
  reframe(count=n())


```



```{r Appendix Figure 2: US-born vs Foreign-born Drop no origin surveys}

## Which ones have origin and which don't?

diag <- surveys %>%
  group_by(origin,
           Survey,
           Year) %>%
  reframe(count=n())

## CCES 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015

yearsurvey_list <- surveys %>%
  group_by(Year,Survey)%>%
  reframe(count=n()) %>%
  filter(Survey %nin% c("CES", "CCES") |
           Year %nin% c(2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015))


  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
## US born first
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 1)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$USborn[1] <- "Born in US"
  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 1)
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$USborn[1] <- "Born in US"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
## CCES 2015 acting weird, figure out later
plot_df <- plot_df %>%
  filter(Year %nin% c(2015) |
         Survey %nin% c("CCES"))

plot_df_usborn <- plot_df


## Foreign Born

plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 0)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$USborn[1] <- "Born Outside US / in Puerto Rico"
  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~USborn,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           USborn == 0)
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$USborn[1] <- "Born Outside US / in Puerto Rico"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
## CCES 2015 acting weird, figure out later
plot_df <- plot_df %>%
  filter(Year %nin% c(2015) |
         Survey %nin% c("CCES"))

plot_df_foreign <- plot_df



## Merge

plot_df <- rbind(plot_df_usborn,
                 plot_df_foreign)
  
  
plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)

plot_df$USborn <- factor(plot_df$USborn,
                         levels = c("Born Outside US / in Puerto Rico",
                                    "Born in US"))

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Dem_se)


plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Rep_se)


plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Ind_se)


## Rebind and plot

plot_df <- rbind(plot_dem_df,
                 plot_rep_df,
                 plot_ind_df)

plot_df <- plot_df %>%
  filter(Survey_N > 0)

## Finalize names

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Ind" ~ "Independent",
    PID == "Rep" ~ "Republican"
  ))

year_range <- unique(yearsurvey_list$Year)
  
fig2 <- ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  # geom_point(aes(size = Survey_N/2000,
  #                color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
  scale_size(guide = "none") + 
  scale_x_continuous(breaks = c(1989, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,80))+
  ylab("% Identifying With Party") +
  theme_bw() +
  facet_wrap(~USborn)+
  theme(legend.position = "top")


ggsave(height = 6, width = 8, filename = "Figures/Appendix/fig2b_pid_bynativity_onlyoriginsurveys.jpg")

## Including point estimates in appendix figure

fig2 <- ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  geom_point(aes(size = Survey_N/2000,
                 color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
  scale_size(guide = "none") + 
  scale_x_continuous(breaks = c(1989, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2023))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,80))+
  ylab("% Identifying With Party") +
  theme_bw() +
  facet_wrap(~USborn)+
  theme(legend.position = "top")


ggsave(height = 6, width = 8, filename = "Figures/Appendix/fig2bb_pid_bynativity_ptestimates_onlyoriginsurveys.jpg")


```



```{r Appendix Figure 3: Generational Include Foreign}

## Create list of surveys to draw from dataset
## Results in individual survey-year point estimates

yearsurveygeneration_list <- surveys %>%
  filter(is.na(USborn)==FALSE) %>%
  group_by(Year,Survey,generation)%>%
  reframe(count=n()) %>%
  filter(is.na(generation)==FALSE) %>%
  
  ## N is too small for these groups, filter out
  filter(generation!= "Greatest") %>%
  filter(generation!= "GenZ" |
         Year %nin% c(2008,2012))


## Remove any without generation data

surveys <- surveys %>%
  filter(is.na(generation)==FALSE)


## Create tribble with ID variables and party ID estimates

  plot_df <- tribble(~Year,~Survey, ~Survey_N,~Generation, ~Dem, ~Dem_var, ~Rep, ~Rep_var, ~Ind, ~Ind_var,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  
## Next loop extracts data using svy package from surveys dataset
## Estimates, per survey-year, each of the three PID categories
  
  ## Start loop dataframe on item 1
  
  year_temp <- yearsurveygeneration_list$Year[1]
  survey_temp <- yearsurveygeneration_list$Survey[1]
  generation_temp <- yearsurveygeneration_list$generation[1]

  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           generation == generation_temp)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem, vartype = c("var","ci")),
            Ind = survey_mean(Ind, vartype = c("var","ci")),
            Rep = survey_mean(Rep, vartype = c("var","ci")),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  plot_df$Generation<-generation_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_var[1] <- year_means$Dem_var[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_var[1] <- year_means$Rep_var[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_var[1] <- year_means$Dem_var[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  
plot_df_bin <- plot_df
plot_df <- plot_df_bin


## Loop over 2:end

for(i in 2:length(yearsurveygeneration_list$Year)){

  plot_temp <- tribble(~Year,~Survey, ~Survey_N,~Generation, 
                       ~Dem, ~Dem_var, 
                       ~Rep, ~Rep_var, 
                       ~Ind, ~Ind_var,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  year_temp <- yearsurveygeneration_list$Year[i]
  survey_temp <- yearsurveygeneration_list$Survey[i]
  generation_temp <- yearsurveygeneration_list$generation[i]
  

  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           generation == generation_temp)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem, vartype = c("var","ci")),
            Ind = survey_mean(Ind, vartype = c("var","ci")),
            Rep = survey_mean(Rep, vartype = c("var","ci")),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp$Generation<-generation_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_var[1] <- year_means$Dem_var[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_var[1] <- year_means$Rep_var[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_var[1] <- year_means$Dem_var[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  
}


## Fix names, factor levels, axis range

plot_df <- plot_df %>%
  mutate(Generation = case_when(
    Generation == "Silent" ~ "Silent (`28-`45)",
    Generation == "Boomer" ~ "Boomer (`46-`64)",
    Generation == "GenX" ~ "GenX (`65-`80)",
    Generation == "Millennial" ~ "Millennial (`81-`96)",
    Generation == "GenZ" ~ "GenZ (`97-)"
  ))

plot_df$Generation <- factor(plot_df$Generation,
                             levels = c("Silent (`28-`45)",
                                        "Boomer (`46-`64)",
                                        "GenX (`65-`80)",
                                        "Millennial (`81-`96)",
                                        "GenZ (`97-)"))


plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(var = case_when(
    PID == "Dem" ~ Dem_var,
    PID == "Ind" ~ Ind_var,
    PID == "Rep" ~ Rep_var
  ))

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Rep" ~ "Republican",
    PID == "Ind" ~ "Independent"
  ))

plot_df$`Party ID` <- factor(plot_df$`Party ID`,
                      levels = c("Democrat",
                                 "Independent",
                                 "Republican"))

year_range <- unique(yearsurvey_list$Year)


fig3 <- ggplot(data = plot_df, 
               aes(x = Year,
               color = `Party ID`
               ))+
  geom_smooth(method = "loess",
              aes(y = value*100,
                  weight = 1/var))+
      ylab("% Identifying With/Leaning Towards Party")+
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020))+
  ylim(c(-10,105))+
  scale_color_manual(values = c("blue2",
                                "green4",
                                "red2"))+
  theme_bw()+
  theme(legend.position = "top")+
  facet_wrap(~Generation, ncol = 1)


fig3

ggsave(height = 8, width = 8, filename = "Figures/Appendix/fig3_bygeneration_includeforeign_genfacet.jpg")




## Only foreign-born

## Create list of surveys to draw from dataset
## Results in individual survey-year point estimates

yearsurveygeneration_list <- surveys %>%
  filter(is.na(USborn)==FALSE) %>%
  group_by(Year,Survey,generation)%>%
  reframe(count=n()) %>%
  filter(is.na(generation)==FALSE) %>%
  
  ## N is too small for these groups, filter out
  filter(generation!= "Greatest") %>%
  filter(generation!= "GenZ" |
         Year %nin% c(2008,2012))


## Remove any without generation data

surveys <- surveys %>%
  filter(is.na(generation)==FALSE)


## Create tribble with ID variables and party ID estimamtes

  plot_df <- tribble(~Year,~Survey, ~Survey_N,~Generation, ~Dem, ~Dem_var, ~Rep, ~Rep_var, ~Ind, ~Ind_var,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  
## Next loop extracts data using svy package from surveys dataset
## Estimates, per survey-year, each of the three PID categories
  
  ## Start loop dataframe on item 1
  
  year_temp <- yearsurveygeneration_list$Year[1]
  survey_temp <- yearsurveygeneration_list$Survey[1]
  generation_temp <- yearsurveygeneration_list$generation[1]

  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           generation == generation_temp,
           USborn == 0)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem, vartype = c("var","ci")),
            Ind = survey_mean(Ind, vartype = c("var","ci")),
            Rep = survey_mean(Rep, vartype = c("var","ci")),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  plot_df$Generation<-generation_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_var[1] <- year_means$Dem_var[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_var[1] <- year_means$Rep_var[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_var[1] <- year_means$Dem_var[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  
plot_df_bin <- plot_df
plot_df <- plot_df_bin


## Loop over 2:end

for(i in 2:length(yearsurveygeneration_list$Year)){

  plot_temp <- tribble(~Year,~Survey, ~Survey_N,~Generation, 
                       ~Dem, ~Dem_var, 
                       ~Rep, ~Rep_var, 
                       ~Ind, ~Ind_var,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  year_temp <- yearsurveygeneration_list$Year[i]
  survey_temp <- yearsurveygeneration_list$Survey[i]
  generation_temp <- yearsurveygeneration_list$generation[i]
  

  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp &
           generation == generation_temp,
           USborn == 0)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem, vartype = c("var","ci")),
            Ind = survey_mean(Ind, vartype = c("var","ci")),
            Rep = survey_mean(Rep, vartype = c("var","ci")),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp$Generation<-generation_temp

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_var[1] <- year_means$Dem_var[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_var[1] <- year_means$Rep_var[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_var[1] <- year_means$Dem_var[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  
}


## Fix names, factor levels, axis range

plot_df <- plot_df %>%
  mutate(Generation = case_when(
    Generation == "Silent" ~ "Silent (`28-`45)",
    Generation == "Boomer" ~ "Boomer (`46-`64)",
    Generation == "GenX" ~ "GenX (`65-`80)",
    Generation == "Millennial" ~ "Millennial (`81-`96)",
    Generation == "GenZ" ~ "GenZ (`97-)"
  ))

plot_df$Generation <- factor(plot_df$Generation,
                             levels = c("Silent (`28-`45)",
                                        "Boomer (`46-`64)",
                                        "GenX (`65-`80)",
                                        "Millennial (`81-`96)",
                                        "GenZ (`97-)"))


plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(var = case_when(
    PID == "Dem" ~ Dem_var,
    PID == "Ind" ~ Ind_var,
    PID == "Rep" ~ Rep_var
  ))

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Rep" ~ "Republican",
    PID == "Ind" ~ "Independent"
  ))

plot_df$`Party ID` <- factor(plot_df$`Party ID`,
                      levels = c("Democrat",
                                 "Independent",
                                 "Republican"))

year_range <- unique(yearsurvey_list$Year)


fig3 <- ggplot(data = plot_df, 
               aes(x = Year,
               color = `Party ID`
               ))+
  geom_smooth(method = "loess",
              aes(y = value*100,
                  weight = 1/var))+
      ylab("% Identifying With/Leaning Towards Party")+
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020))+
  ylim(c(-10,105))+
  scale_color_manual(values = c("blue2",
                                "green4",
                                "red2"))+
  theme_bw()+
  theme(legend.position = "top")+
  facet_wrap(~Generation, ncol = 1)


fig3

ggsave(height = 8, width = 8, filename = "Figures/Appendix/fig3_bygeneration_onlyforeign_genfacet.jpg")



```



```{r Appendix Figure 4: National Origin Only Foreign-Born}


diag_df <- surveys %>%
  group_by(Survey,
           origin) %>%
  reframe(count=n())

yearsurvey_list <- surveys %>%
  group_by(Year,Survey)%>%
  reframe(count=n())

## Mexican first

  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Mexican",
           USborn==0)
  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$Origin[1] <- "Mexican"

  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Mexican",
                      USborn==0)

  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$Origin[1] <- "Mexican"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
plot_mex_df <- plot_df
  
  
## Then Puerto Rican
  
    plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Puerto Rican",
                      USborn==0)

  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$Origin[1] <- "Puerto Rican"

  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Puerto Rican",
                     USborn==0)

  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$Origin[1] <- "Puerto Rican"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
plot_pr_df <- plot_df
  
  
  
## Lastly Cuban

  plot_df <- tribble(~Year,~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Cuban",
                     USborn==0)

  
  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_df$Year <- year_temp
  plot_df$Survey<-survey_temp
  
  plot_df$Dem[1] <- as.numeric(year_means$Dem[1])
  plot_df$Dem_se[1] <- year_means$Dem_se[1]
  plot_df$Rep[1] <- year_means$Rep[1]
  plot_df$Rep_se[1] <- year_means$Rep_se[1]
  plot_df$Ind[1] <- year_means$Ind[1]
  plot_df$Ind_se[1] <- year_means$Dem_se[1]
  plot_df$Survey_N[1] <- year_means$Survey_N[1]
  plot_df$Origin[1] <- "Cuban"

  

for(i in 2:length(yearsurvey_list$Year)){

  plot_temp <- tribble(~Year, ~Survey, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se, ~Origin,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           origin == "Cuban",
                     USborn==0)

  
  year_means <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Year = year_temp,
            Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n())
  
  plot_temp$Year <- year_temp
  plot_temp$Survey<-survey_temp
  plot_temp <- plot_temp %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp$Dem[1] <- year_means$Dem[1]
  plot_temp$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp$Rep[1] <- year_means$Rep[1]
  plot_temp$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp$Ind[1] <- year_means$Ind[1]
  plot_temp$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp$Origin[1] <- "Cuban"
  
  plot_df <- rbind(plot_df,
                   plot_temp)
  

}
  
plot_cuba_df <- plot_df


plot_df <- rbind(plot_mex_df,
                 plot_pr_df,
                 plot_cuba_df)

  
## Drop all results missing Natl Origin

plot_df <- plot_df %>%
  filter(Survey_N > 0)
  
  
plot_df <- pivot_longer(plot_df,
                          names_to = "PID",
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Dem_se)


plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Rep_se)


plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) %>%
  mutate(se = Ind_se)


## Rebind and plot

plot_df <- rbind(plot_dem_df,
                 plot_rep_df,
                 plot_ind_df)

## Finalize names

plot_df <- plot_df %>%
  mutate(`Party ID` = case_when(
    PID == "Dem" ~ "Democrat",
    PID == "Ind" ~ "Independent",
    PID == "Rep" ~ "Republican"
  ))

plot_df$Origin <- factor(plot_df$Origin,
                         levels = c("Mexican",
                                    "Puerto Rican",
                                    "Cuban"))

year_range <- unique(yearsurvey_list$Year)
  
ggplot(data = plot_df,
               aes(x = Year,
                   y = value)) +
  # geom_point(aes(size = Survey_N/2000,
  #                color = `Party ID`)) +
  geom_smooth(aes(weight = 1/se, 
                  color = `Party ID`),
              method = "loess") +
  scale_size(guide = "none") + 
    geom_line(y = 0,
         color = "black",
         linetype = "dashed") +
  scale_x_continuous(breaks = c(1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020))+
  scale_color_manual(values = c("blue2","green4","red3"))+
  ylim(c(-10,100))+
  ylab("% Identifying With/Leaning Towards Party") +
  theme_bw() +
  facet_wrap(~Origin)+
  theme(legend.position = "top",
        axis.text.x = element_text(size = 7))


ggsave(height = 7, width = 10, filename = "Figures/Appendix/fig4a_pid_bynatlorigin_onlyforeign.jpg")



```


```{r Appendix: Trends in population size for Census}

# Census trends:
## Load data

## Loading each individually to ensure the data is structured properly before merging all together

census1 <- read_csv(file = "PUMS Population Proportions/Census_1990_Counts2.csv")
census2 <- read_csv(file = "PUMS Population Proportions/Census_2000_Counts2.csv")
census3 <- read_csv(file = "PUMS Population Proportions/PUMS_2001_Counts2.csv")
census4 <- read_csv(file = "PUMS Population Proportions/PUMS_2002_Counts2.csv")
census5 <- read_csv(file = "PUMS Population Proportions/PUMS_2003_Counts2.csv")
census6 <- read_csv(file = "PUMS Population Proportions/PUMS_2004_Counts2.csv")
census7 <- read_csv(file = "PUMS Population Proportions/PUMS_2005_Counts2.csv")
census8 <- read_csv(file = "PUMS Population Proportions/PUMS_2006_Counts2.csv")
census9 <- read_csv(file = "PUMS Population Proportions/PUMS_2007_Counts2.csv")
census10 <- read_csv(file = "PUMS Population Proportions/PUMS_2008_Counts2.csv")
census11 <- read_csv(file = "PUMS Population Proportions/PUMS_2009_Counts2.csv")
census12 <- read_csv(file = "PUMS Population Proportions/PUMS_2010_Counts2.csv")
census13 <- read_csv(file = "PUMS Population Proportions/PUMS_2011_Counts2.csv")
census14 <- read_csv(file = "PUMS Population Proportions/PUMS_2012_Counts2.csv")
census15 <- read_csv(file = "PUMS Population Proportions/PUMS_2013_Counts2.csv")
census16 <- read_csv(file = "PUMS Population Proportions/PUMS_2014_Counts2.csv")
census17 <- read_csv(file = "PUMS Population Proportions/PUMS_2015_Counts2.csv")
census18 <- read_csv(file = "PUMS Population Proportions/PUMS_2016_Counts2.csv")
census19 <- read_csv(file = "PUMS Population Proportions/PUMS_2017_Counts2.csv")
census20 <- read_csv(file = "PUMS Population Proportions/PUMS_2018_Counts2.csv")
census21 <- read_csv(file = "PUMS Population Proportions/PUMS_2019_Counts2.csv")
census22 <- read_csv(file = "PUMS Population Proportions/PUMS_2020_Counts2.csv")
census23 <- read_csv(file = "PUMS Population Proportions/PUMS_2021_Counts2.csv")
census24 <- read_csv(file = "PUMS Population Proportions/PUMS_2022_Counts2.csv")

census <- rbind(census1, census2, census3, census4, census5, census6,
                     census7, census8, census9, census10, census11, census12,
                     census13, census14, census15, census16, census17, census18,
                     census19, census20, census21, census22, census23, census24)

rm(census1, census2, census3, census4, census5, census6,
                     census7, census8, census9, census10, census11, census12,
                     census13, census14, census15, census16, census17, census18,
                     census19, census20, census21, census22, census23, census24)


## Checking national origin

diag_df <- census %>%
  group_by(origin,
           year) %>%
  reframe(count=n())

## Group by: Year, USborn, and Origin

plot_df <- census %>%
  group_by(year, 
           USborn,
           origin) %>%
  reframe(value = sum(WeightedN, na.rm=TRUE)/1000000)

## Changing DV to be proportion of total Latino population
proportion_df <- plot_df %>% 
  ungroup() %>%
  group_by(year,
           USborn) %>%
  mutate(total = sum(value, na.rm = TRUE)) %>%
  select(-origin, -value)

plot_df <- left_join(plot_df,
                     proportion_df,
                     by = c("year", "USborn"))

plot_df <- plot_df %>%
  mutate(prop = value*100/total)

## Fixing final factor and labels

plot_df <- plot_df %>%
  mutate(origin = case_when(
    origin == "Salv" ~ "Salvadoran",
    origin == "Dom" ~ "Dominican",
    origin == "Cub" ~ "Cuban",
    origin == "Mex" ~ "Mexican",
    origin == "PR" ~ "Puerto Rican",
    TRUE ~ origin
  ))

plot_df$Origin <- factor(plot_df$origin,
                         levels = c("Mexican",
                                    "Puerto Rican",
                                    "Cuban",
                                    "Dominican",
                                    "Salvadoran",
                                    "Other"))



plot_df <- plot_df %>%
  mutate(Nativity = ifelse(USborn == 0,
                              "Foreign/PR-Born",
                              "US-Born"))

plot_df <- plot_df %>%
  mutate(Year = year)

## Plot


year_range <- unique(plot_df$year)
  
ggplot(data = plot_df,
               aes(x = Year,
                   y = prop,
                   color = Origin)) +
  geom_smooth(type = "loess") +
  scale_x_continuous(breaks = c(1988, 1996, 2004, 2012, 2020))+
  scale_y_continuous(breaks = c(0,5,10,25,50,75,100))+
  ylim(c(0,100))+
  ylab("Percent of Latino Population") +
  theme_bw() +
  facet_grid(Nativity~Origin)+
  theme(legend.position = "top",
        axis.text.x = element_text(size = 7))


ggsave(height = 7, width = 12, filename = "Figures/Appendix/fig_natlorigintrends_censusdata.jpg")



```


```{r Appendix: Trends in population for survey data}

# Trends in survey data:


plot_df <- surveys %>%
  group_by(Year, 
           USborn,
           origin) %>%
  reframe(value = n())

plot_df <- plot_df %>%
  filter(is.na(USborn)==FALSE) %>%
  filter(is.na(origin)==FALSE)


## Changing DV to be proportion of total survey sample
proportion_df <- plot_df %>% 
  ungroup() %>%
  group_by(Year,
           USborn) %>%
  mutate(total = sum(value, na.rm = TRUE)) %>%
  select(-origin, -value)

plot_df <- left_join(plot_df,
                     proportion_df,
                     by = c("Year", "USborn"))

plot_df <- plot_df %>%
  mutate(prop = value*100/total)


## Final factors

plot_df <- plot_df %>%
  mutate(Nativity = ifelse(USborn == 0,
                              "Foreign/PR-Born",
                              "US-Born"))




plot_df$Origin <- factor(plot_df$origin,
                         levels = c("Mexican",
                                    "Puerto Rican",
                                    "Cuban",
                                    "Dominican",
                                    "Salvadoran",
                                    "Other"))

ggplot(data = plot_df,
               aes(x = Year,
                   y = prop,
                   color = Origin)) +
  geom_smooth(type = "loess") +
  scale_x_continuous(breaks = c(1988, 1996, 2004, 2012, 2020))+
  ylab("Percent of Survey Respondents per Year") +
  ylim(c(0,100))+
  theme_bw() +
  facet_grid(Nativity~Origin)+
  theme(legend.position = "top",
        axis.text.x = element_text(size = 7))


ggsave(height = 7, width = 12, filename = "Figures/Appendix/fig_natlorigintrends_surveysdata.jpg")



```


```{r Appendix: State-level change check}

## How is PID shifting for White/Black/Latino, in majority-Latino states?

## CA, TX, FL, NY

## Using CES 2012, 2016, 2020 data 

ces_06to23 <- readRDS("Data/cumulative_2006-2023.rds")

names(ces_06to23)

## Check state 

diag_df <- ces_06to23 %>%
  group_by(st) %>%
  reframe(count=n())


## Check and ensnure pid3_leaner is our specifications

diag_df <- ces_06to23 %>%
  group_by(pid3_leaner, pid7) %>%
  reframe(count=n())

## Good; PID 1 = Dem, PID 2 = Rep, PID 3 = true ind

## Check race and race_h

diag_df <- ces_06to23 %>%
  filter(year > 2011) %>%
  group_by(race, race_h, hispanic) %>%
  reframe(count=n())


## Make smaller dataframe for plot

plot_df <- ces_06to23 %>%
  filter(st %in% c("TX","CA","FL","NY"))%>%
  filter(race %in% c(1,2,3)) %>%
  filter(pid3_leaner %in% c(1,2,3)) %>%
  group_by(year, st, race, pid3_leaner) %>%
  reframe(count=n())
  

## Get percentage by group

plot_df <- plot_df %>%
  mutate(Dem = ifelse(pid3_leaner == 1,
                      1,
                      0),
         Rep = ifelse(pid3_leaner == 2,
                      1,
                      0),
         Ind = ifelse(pid3_leaner == 3,
                      1,
                      0)
  )

plot_df <- plot_df %>%
  group_by(year, st, race) %>%
  reframe(fullcount = sum(count),
          Dem = Dem*count,
          Rep = Rep*count,
          Ind = Ind*count)

plot_df <- plot_df %>%
  pivot_longer(cols = c(Dem, Rep, Ind),
               names_to = "PID") %>%
  mutate(Percent = value/fullcount) 

plot_df <- plot_df %>%
  filter(value>0)



## Fix names

plot_df <- plot_df %>%
  mutate(Race = case_when(
    race == 1 ~ "White",
    race == 2 ~ "Black",
    race == 3 ~ "Latino")
  )



## Plot

ggplot(data = plot_df,
       aes(x = year,
           y = Percent,
           color = PID)) +
  geom_smooth(type = "loess") +
  scale_color_manual(values = c("blue2","green3","red3"))+
  facet_grid(st~Race)

ggsave(height = 8, width = 10, "Figures/Appendix/cces_06to23_statepid.png")

```


```{r Appendix: Figure 1 Weights check}

## Figure 1, All respondents

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp)
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp)
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}


  
plot_df <- pivot_longer(plot_df,
                          names_to = c("PID"),
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)


plot_df$Weights <- factor(plot_df$Weights,
                          levels = c("None",
                                     "Original",
                                     "Post-Strat.",
                                     "Post-Strat - None",
                                     "Post-Strat - Original"))

plot_df_comparisons <- plot_df %>%
  filter(Weights %in% c("Post-Strat - None",
                         "Post-Strat - Original"))


plot_df <- plot_df %>%
  filter(Weights %nin% c("Post-Strat - None",
                         "Post-Strat - Original"))

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) 

plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))

plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))


year_range <- unique(yearsurvey_list$Year)


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_wrap(~Weights) + 
  geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
    ylim(c(0,80))+
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_wrap(~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
    ylim(c(0,80))+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_wrap(~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  ylim(c(0,80))+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 10, width = 10, filename = "Figures/Appendix/fig1_pid_fullsample_weights.jpg")



## Weights comparisons

year_range <- unique(yearsurvey_list$Year)

plot_dem_df <- plot_df_comparisons %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  filter(Year %nin% c(1989,1999))

plot_rep_df <- plot_df_comparisons %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se) %>%
  filter(Year %nin% c(1989,1999))


plot_ind_df <- plot_df_comparisons %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se) %>%
  filter(Year %nin% c(1989,1999))


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_wrap(~Weights) + 
  geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
  geom_line(y = 0)+
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_wrap(~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_wrap(~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p1 / p2 / p3 


ggsave(height = 10, width = 10, filename = "Figures/Appendix/fig1_pid_fullsample_comparisonweights.jpg")




```


```{r Appendix: Figure 2 Weights check}

## Figure 2, By nativity

## First, US-born only

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1)
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1)
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot1_df <- plot_df


## Then, foreign-born only

## First, US-born only

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==0)
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 0)
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Nativity <- "Foreign/PR Born"
plot1_df$Nativity<- "US Born"

plot_df <- rbind(plot1_df,
                 plot_df)


plot_df <- pivot_longer(plot_df,
                          names_to = c("PID"),
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)


plot_df$Weights <- factor(plot_df$Weights,
                          levels = c("None",
                                     "Original",
                                     "Post-Strat.",
                                     "Post-Strat - None",
                                     "Post-Strat - Original"))

plot_df_comparisons <- plot_df %>%
  filter(Weights %in% c("Post-Strat - None",
                         "Post-Strat - Original")) %>%
  filter(Year %nin% c(1989, 1999))


plot_df <- plot_df %>%
  filter(Weights %nin% c("Post-Strat - None",
                         "Post-Strat - Original"))


## Removing issue years

plot_df <- plot_df %>%
  filter(value>0)

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) 

plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))

plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))


year_range <- unique(yearsurvey_list$Year)


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Nativity~Weights) + 
  geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
    ylim(c(0,80))+
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Nativity~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
    ylim(c(0,80))+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Nativity~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  ylim(c(0,80))+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 14, width = 10, filename = "Figures/Appendix/fig2_pid_bynativity_weights.jpg")



## Weights comparisons

year_range <- unique(yearsurvey_list$Year)

plot_dem_df <- plot_df_comparisons %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  filter(Year %nin% c(1989,1999))

plot_rep_df <- plot_df_comparisons %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se) %>%
  filter(Year %nin% c(1989,1999))


plot_ind_df <- plot_df_comparisons %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se) %>%
  filter(Year %nin% c(1989,1999))


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
    geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
  geom_line(y = 0)+
  facet_grid(Nativity~Weights) + 
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_grid(Nativity~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_grid(Nativity~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 10, width = 10, filename = "Figures/Appendix/fig2_pid_usborn_comparisonweights.jpg")


```


```{r Appendix: Figure 3 Weights check}

## Figure 3, By Generation

diag <- surveys %>%
  group_by(generation) %>%
  reframe(count=n())

## First, Silents

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           generation == "Silent")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           generation == "Silent")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Generation <- "Silent"

plot1_df <- plot_df



## Second, Boomers

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           generation == "Boomer")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           generation == "Boomer")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Generation <- "Boomer"

plot2_df <- plot_df



## Third, GenX

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           generation == "GenX")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           generation == "GenX")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Generation <- "GenX"

plot3_df <- plot_df



## Fourth, Millennial

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           generation == "Millennial")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           generation == "Millennial")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Generation <- "Millennial"

plot4_df <- plot_df



## Fifth, GenZ

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           generation == "GenZ")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           generation == "GenZ")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Generation <- "GenZ"

plot5_df <- plot_df


## Merge

plot_df <- rbind(plot1_df,
                 plot2_df,
                 plot3_df,
                 plot4_df,
                 plot5_df)


plot_df <- pivot_longer(plot_df,
                          names_to = c("PID"),
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)


plot_df$Weights <- factor(plot_df$Weights,
                          levels = c("None",
                                     "Original",
                                     "Post-Strat.",
                                     "Post-Strat - None",
                                     "Post-Strat - Original"))

plot_df$Generation <- factor(plot_df$Generation,
                             levels = c("Silent",
                                        "Boomer",
                                        "GenX",
                                        "Millennial",
                                        "GenZ"))


## Splitting

plot_df_comparisons <- plot_df %>%
  filter(Weights %in% c("Post-Strat - None",
                         "Post-Strat - Original")) %>%
  filter(Year %nin% c(1989, 1999))


plot_df <- plot_df %>%
  filter(Weights %nin% c("Post-Strat - None",
                         "Post-Strat - Original"))



## Removing issue years

plot_df <- plot_df %>%
  filter(value>0)

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) 

plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))

plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))


year_range <- unique(yearsurvey_list$Year)


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Generation~Weights) + 
  geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
    ylim(c(0,80))+
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Generation~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
    ylim(c(0,80))+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Generation~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  ylim(c(0,80))+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 14, width = 10, filename = "Figures/Appendix/fig3_pid_bygeneration_weights.jpg")



## Weights comparisons

year_range <- unique(yearsurvey_list$Year)

plot_dem_df <- plot_df_comparisons %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  filter(Year %nin% c(1989,1999))

plot_rep_df <- plot_df_comparisons %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se) %>%
  filter(Year %nin% c(1989,1999))


plot_ind_df <- plot_df_comparisons %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se) %>%
  filter(Year %nin% c(1989,1999))


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
    geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
  geom_line(y = 0)+
  facet_grid(Generation~Weights) + 
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_grid(Generation~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_grid(Generation~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 10, width = 10, filename = "Figures/Appendix/fig3_pid_usborn_comparisonweights.jpg")


```


```{r Appendix: Figure 4 Weights check}

## Figure 4, By Generation

diag <- surveys %>%
  group_by(origin) %>%
  reframe(count=n())

## First, Mexican origin

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           origin=="Mexican")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           origin == "Mexican")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Origin <- "Mexico"

plot1_df <- plot_df



## Second, Puerto Ricans

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           origin == "Mexican")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           origin == "Mexican")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Origin <- "Puerto Rico"

plot2_df <- plot_df



## Third, Cuban

## Party ID all superimposed
## Point estimates in back: Size = N, with confidence intervals


plot_df <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp1 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp2 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp3 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp4 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

plot_temp5 <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  
  i=1
  
  year_temp <- yearsurvey_list$Year[1]
  survey_temp <- yearsurvey_list$Survey[1]
  plot_df$Year_Survey[1] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn==1,
           origin == "Cuban")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se - year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


plot_df <- plot_temp


for(i in 2:length(yearsurvey_list$Year)){
  
  
plot_temp <- tribble(~Year,~Survey, ~Weights, ~Survey_N, ~Dem, ~Dem_se, ~Rep, ~Rep_se, ~Ind, ~Ind_se,
                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

  year_temp <- yearsurvey_list$Year[i]
  survey_temp <- yearsurvey_list$Survey[i]
  plot_df$Year_Survey[i] <- paste(survey_temp,
                                  year_temp)
  
  surveys_temp <- surveys %>%
    filter(Year == year_temp &
           Survey== survey_temp,
           USborn == 1,
           origin == "Cuban")
  
  
  year_means1 <- surveys_temp %>%
    as_survey(weights = PSweights) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
          mutate(Weights = "Post-Strat.")
  
    year_means2 <- surveys_temp %>%
    as_survey(weights = Weight) %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
            mutate(Weights = "Original")
    
    year_means3 <- surveys_temp %>%
    as_survey() %>%
    reframe(Dem = survey_mean(Dem),
            Ind = survey_mean(Ind),
            Rep = survey_mean(Rep),
            Survey_N = n()) %>%
      mutate(Weights = "None")
    
    year_means4 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means4$Dem <- year_means1$Dem - year_means3$Dem
    year_means4$Dem_se <- year_means1$Dem_se - year_means3$Dem_se
    year_means4$Rep <- year_means1$Rep - year_means3$Rep
    year_means4$Rep_se <- year_means1$Rep_se- year_means3$Rep_se
    year_means4$Ind <- year_means1$Ind - year_means3$Ind
    year_means4$Ind_se <- year_means1$Ind_se - year_means3$Ind_se
    year_means4$Survey_N <- year_means1$Survey_N
    year_means4$Weights <- "Post-Strat - None"
    
    year_means5 <- tribble(~Dem, ~Dem_se, ~Ind, ~Ind_se, ~Rep, ~Rep_se, ~Survey_N, ~Weights,
                           NA, NA, NA, NA, NA, NA, NA, NA)
    year_means5$Dem <- year_means1$Dem - year_means2$Dem
    year_means5$Dem_se <- year_means1$Dem_se - year_means2$Dem_se
    year_means5$Rep <- year_means1$Rep - year_means2$Rep
    year_means5$Rep_se <- year_means1$Rep_se- year_means2$Rep_se
    year_means5$Ind <- year_means1$Ind - year_means2$Ind
    year_means5$Ind_se <- year_means1$Ind_se - year_means2$Ind_se
    year_means5$Survey_N <- year_means1$Survey_N
    year_means5$Weights <- "Post-Strat - Original"
    
year_means <- rbind(year_means1,
                    year_means2,
                    year_means3,
                    year_means4,
                    year_means5)

  plot_temp1$Year <- year_temp
  plot_temp1$Survey<-survey_temp
  plot_temp1 <- plot_temp1 %>%
    mutate(Year_Survey = paste(Survey, Year))

  plot_temp1$Dem[1] <- year_means$Dem[1]
  plot_temp1$Dem_se[1] <- year_means$Dem_se[1]
  plot_temp1$Rep[1] <- year_means$Rep[1]
  plot_temp1$Rep_se[1] <- year_means$Rep_se[1]
  plot_temp1$Ind[1] <- year_means$Ind[1]
  plot_temp1$Ind_se[1] <- year_means$Dem_se[1]
  plot_temp1$Survey_N[1] <- year_means$Survey_N[1]
  plot_temp1$Weights[1] <- year_means$Weights[1]
  
  plot_temp2$Year <- year_temp
  plot_temp2$Survey<-survey_temp
  plot_temp2 <- plot_temp2 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp2$Dem[1] <- year_means$Dem[2]
  plot_temp2$Dem_se[1] <- year_means$Dem_se[2]
  plot_temp2$Rep[1] <- year_means$Rep[2]
  plot_temp2$Rep_se[1] <- year_means$Rep_se[2]
  plot_temp2$Ind[1] <- year_means$Ind[2]
  plot_temp2$Ind_se[1] <- year_means$Dem_se[2]
  plot_temp2$Survey_N[1] <- year_means$Survey_N[2]
  plot_temp2$Weights[1] <- year_means$Weights[2]
  
  plot_temp3$Year <- year_temp
  plot_temp3$Survey<-survey_temp
  plot_temp3 <- plot_temp3 %>%
    mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp3$Dem[1] <- year_means$Dem[3]
  plot_temp3$Dem_se[1] <- year_means$Dem_se[3]
  plot_temp3$Rep[1] <- year_means$Rep[3]
  plot_temp3$Rep_se[1] <- year_means$Rep_se[3]
  plot_temp3$Ind[1] <- year_means$Ind[3]
  plot_temp3$Ind_se[1] <- year_means$Dem_se[3]
  plot_temp3$Survey_N[1] <- year_means$Survey_N[3]
  plot_temp3$Weights[1] <- year_means$Weights[3]
  
  plot_temp4$Year <- year_temp
  plot_temp4$Survey<-survey_temp
  plot_temp4 <- plot_temp4 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp4$Dem[1] <- year_means$Dem[4]
  plot_temp4$Dem_se[1] <- year_means$Dem_se[4]
  plot_temp4$Rep[1] <- year_means$Rep[4]
  plot_temp4$Rep_se[1] <- year_means$Rep_se[4]
  plot_temp4$Ind[1] <- year_means$Ind[4]
  plot_temp4$Ind_se[1] <- year_means$Dem_se[4]
  plot_temp4$Survey_N[1] <- year_means$Survey_N[4]
  plot_temp4$Weights[1] <- year_means$Weights[4]
  
  plot_temp5$Year <- year_temp
  plot_temp5$Survey<-survey_temp
  plot_temp5 <- plot_temp5 %>%
  mutate(Year_Survey = paste(Survey, Year))
  
  plot_temp5$Dem[1] <- year_means$Dem[5]
  plot_temp5$Dem_se[1] <- year_means$Dem_se[5]
  plot_temp5$Rep[1] <- year_means$Rep[5]
  plot_temp5$Rep_se[1] <- year_means$Rep_se[5]
  plot_temp5$Ind[1] <- year_means$Ind[5]
  plot_temp5$Ind_se[1] <- year_means$Dem_se[5]
  plot_temp5$Survey_N[1] <- year_means$Survey_N[5]
  plot_temp5$Weights[1] <- year_means$Weights[5]
  
  
  plot_temp <- rbind(plot_temp1,
                     plot_temp2,
                     plot_temp3,
                     plot_temp4,
                     plot_temp5)


  plot_df <- rbind(plot_df,
                   plot_temp)

}

plot_df$Origin <- "Cuba"

plot3_df <- plot_df




## Merge

plot_df <- rbind(plot1_df,
                 plot2_df,
                 plot3_df)


plot_df <- pivot_longer(plot_df,
                          names_to = c("PID"),
                          cols = c(Dem, Rep, Ind))

plot_df <- plot_df %>%
  mutate(value = value*100,
         Dem_se = Dem_se*100,
         Rep_se = Rep_se*100,
         Ind_se = Ind_se*100)


plot_df$Weights <- factor(plot_df$Weights,
                          levels = c("None",
                                     "Original",
                                     "Post-Strat.",
                                     "Post-Strat - None",
                                     "Post-Strat - Original"))

plot_df$Origin <- factor(plot_df$Origin,
                             levels = c("Mexico",
                                        "Puerto Rico",
                                        "Cuba"))


## Splitting

plot_df_comparisons <- plot_df %>%
  filter(Weights %in% c("Post-Strat - None",
                         "Post-Strat - Original")) %>%
  filter(Year %nin% c(1989, 1999))


plot_df <- plot_df %>%
  filter(Weights %nin% c("Post-Strat - None",
                         "Post-Strat - Original"))



## Removing issue years

plot_df <- plot_df %>%
  filter(value>0)

## Point estimates behind

plot_dem_df <- plot_df %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci)) 

plot_rep_df <- plot_df %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))

plot_ind_df <- plot_df %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se)%>%
  mutate(lower_ci = ifelse(lower_ci < 0,
                           0,
                           lower_ci))


year_range <- unique(yearsurvey_list$Year)


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Origin~Weights) + 
  geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
    ylim(c(0,80))+
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Origin~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
    ylim(c(0,80))+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/2000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  geom_errorbar(aes(ymin = lower_ci,
                    ymax = upper_ci),
                size = 0.5,
                color = "gray50") +
  facet_grid(Origin~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  ylim(c(0,80))+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 14, width = 10, filename = "Figures/Appendix/fig4_pid_natlorigin_weights.jpg")



## Weights comparisons

year_range <- unique(yearsurvey_list$Year)

plot_dem_df <- plot_df_comparisons %>%
  filter(PID == "Dem") %>%
  mutate(upper_ci = value + Dem_se,
         lower_ci = value - Dem_se) %>%
  filter(Year %nin% c(1989,1999))

plot_rep_df <- plot_df_comparisons %>%
  filter(PID == "Rep") %>%
  mutate(upper_ci = value + Rep_se,
         lower_ci = value - Rep_se) %>%
  filter(Year %nin% c(1989,1999))


plot_ind_df <- plot_df_comparisons %>%
  filter(PID == "Ind") %>%
  mutate(upper_ci = value + Ind_se,
         lower_ci = value - Ind_se) %>%
  filter(Year %nin% c(1989,1999))


p1 <- ggplot(data = plot_dem_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "blue3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
    geom_smooth(aes(weight = 1/Dem_se,
                  y = value),
              method = "loess",
              color = "blue3")+
  geom_line(y = 0)+
  facet_grid(Origin~Weights) + 
  theme(legend.position = "none")

p2 <- ggplot(data = plot_ind_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "green4",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_grid(Origin~Weights) + 
  geom_smooth(aes(weight = 1/Ind_se,
                  y = value),
              method = "loess",
              color = "green4")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p3 <- ggplot(data = plot_rep_df,
                     aes(x = Year))+
  geom_point(aes(size = Survey_N/20000,
                 y = value,
                 x = Year,
                 fill = Year_Survey),
             color = "red3",
             alpha = 0.5) + 
  # geom_errorbar(aes(ymin = lower_ci,
  #                   ymax = upper_ci),
  #               size = 0.5,
  #               color = "gray50") +
  facet_grid(Origin~Weights) + 
  geom_smooth(aes(weight = 1/Rep_se,
                  y = value),
              method = "loess",
              color = "red3")+
  geom_line(y = 0)+
  theme(legend.position = "none")


p1 / p2 / p3


ggsave(height = 10, width = 10, filename = "Figures/Appendix/fig4_pid_natlorigin_comparisonweights.jpg")


```


